#pragma once
#include<iostream>
#include"Protocol.hpp"

enum{
    Div_Zero=1,
    Mod_Zero,
    Other_Oper
};
class ServerCal
{
public:
    ServerCal()
    {}
    ~ServerCal()
    {}
    Response CalculatopHelper(const Request&req)
    {
        Response res;
        switch (req.op)
        {
        case '+':
            res.result=req.x+req.y;
            break;
        case '-':
            res.result=req.x-req.y;
            break;
        case '*':
            res.result=req.x*req.y;
            break;
        case '/':
            if(req.y==0)
                res.code=Div_Zero;
            else
                res.result=req.x/req.y;
            break; 
        case '%':
            if(req.y==0)
                res.code=Mod_Zero;
            else
                res.result=req.x%req.y;
            break; 
        default:
            res.code=Other_Oper;
            break;
        }
        return res;
    }
    std::string Calculator(std::string &package)
    {
        std::string content;
        bool r=Decode(package,&content);
        if(!r)
            return "";
        Request req;
        r=req.Deserialize(content);
        std::cout<<"收到一个请求";
        req.Print_req();
        if (!r)
            return "";
        content="";
        Response res=CalculatopHelper(req);
        std::cout<<"发送一个回复";
        res.Print_result();
        res.Serialize(&content);
        content=Encode(content);
        return content;
    }
private:

};






